/**
 * 手工发布数据到ERP系统集成的中间表
 * 
 * @author WeiWenJie
 * @Version 1.0
 * @Date 2012-08-30
 */

package ext.generic.erp.manually;

import com.ptc.core.components.beans.ObjectBean;
import com.ptc.core.components.forms.DefaultObjectFormProcessor;
import com.ptc.core.components.forms.FormProcessingStatus;
import com.ptc.core.components.forms.FormResult;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.beans.NmCommandBean;
import com.ptc.windchill.enterprise.util.PartManagementHelper;
import java.util.List;
import wt.part.WTPart;
import wt.util.WTException;
import wt.vc.views.*;
import ext.generic.erp.common.CommonUtil;
import ext.generic.erp.service.PartReleaseService;
import ext.generic.erp.service.BOMReleaseService;
import ext.generic.erp.util.PDMIntegrationLogUtil;

public class ErpManuallyPublishProcessor extends DefaultObjectFormProcessor {
	private static final String RESOURCE = IntegrationResource.class.getName();
	private static final String GET_RADIO_FAILD = "integration.erpManuallyPublish_step.GET_RADIO_FAILD" ;
	
	private static final String clazz = ErpManuallyPublishProcessor.class.getName() ;
	
	public ErpManuallyPublishProcessor() {
		
	}

	
	public FormResult doOperation(NmCommandBean nmcommandbean, List list) throws WTException {
		return super.doOperation( nmcommandbean, list);
	}

	
	public FormResult preProcess(NmCommandBean nmcommandbean, List list) throws WTException {
		PDMIntegrationLogUtil.printLog(clazz, "=====> 开始执行手工发布BOM或者物料到ERP系统") ;
		
		NmOid nmoid = ((ObjectBean) list.get(0)).getActionOid();
		WTPart wtpart = (WTPart) nmoid.getRef();
//		WTUser user = (WTUser) SessionHelper.manager.getPrincipalReference().getObject();
//		WTPrincipal curUser = SessionHelper.manager.getPrincipal();
		
		try {
			String selectType = getSelectedType(nmcommandbean);
			
			String releaseTime = CommonUtil.getCurrentTime() ;
			
			//执行手工发布
			if (selectType.equalsIgnoreCase("bom")) {
				// 发布BOM结构
				PDMIntegrationLogUtil.printLog(clazz, "=====> 选择手工发布BOM") ;
				BOMReleaseService.releaseAllLevel(wtpart, releaseTime, null);
			}else if (selectType.equalsIgnoreCase("part")) {
				// 发布物料信息
				PDMIntegrationLogUtil.printLog(clazz, "=====> 选择手工发布物料") ;
				PartReleaseService.release(wtpart, releaseTime);
			}
		} catch (ViewException viewexception) {
			throw new WTException(viewexception);
		} finally{
			PDMIntegrationLogUtil.printLog(clazz, "=====> 结束手工发布BOM或者物料到ERP系统") ;
		}

		return new FormResult(FormProcessingStatus.SUCCESS);
	}

	
//	private FormResult getFailureFeedbackResult(Object aobj[], String s,WTException wtexception) throws WTException {
//		String s1 = WTMessage.getLocalizedMessage(
//				"ext.itech.erp.manually.publish.ErpManuallyPublishResource", s,
//				aobj, locale);
//		String s2 = null;
//		if (wtexception != null) {
//			s2 = wtexception.getLocalizedMessage(locale, true);
//		}
//		FeedbackMessage feedbackmessage = new FeedbackMessage(
//				FeedbackType.FAILURE, locale, s1, null, new String[] { s2 });
//		FormResult formresult = new FormResult(FormProcessingStatus.FAILURE);
//		formresult.addFeedbackMessage(feedbackmessage);
//		formresult.setNextAction(FormResultAction.NONE);
//		return formresult;
//	}
	

	// 取得单选框“操作类型”的选择值
	private String getSelectedType(NmCommandBean nmcommandbean)
			throws WTException {
		String selectType = PartManagementHelper.getRadioButtonValueFromForm(nmcommandbean, "typeradio");
		if (selectType == null){
			throw new WTException( RESOURCE , GET_RADIO_FAILD , null);
		}else{
			PDMIntegrationLogUtil.printLog(clazz, "=====> 手工发布BOM或者物料到ERP系统，选择的类型为：" + selectType) ;
			return selectType;
		}
	}
}